$(document).ready(function (){

    function ajaxCall(method, url, reqData, callback, errorback){
        $.ajax({
            method: method,
            url: url,
            contentType: 'application/x-www-form-urlencoded',
            data: reqData.toString(),
            success: (respData) =>{
                if(callback){
                    callback(respData)
                }else{
                    console.log(respData)
                }
            },
            error: (err) => {
                if(errorback){
                    errorback(err)
                }else{
                    window.location.href = 'xxx.html'
                }
            }
        })
    }
    function ajaxGet(url, reqData, callback, errorback ){
        ajaxCall("GET", url, reqData, callback, errorback)
    }
    function ajaxPost(url, reqData, callback, errorback ){
        ajaxCall("POST", url, reqData, callback, errorback)
    }
    function login(){
        let userName = $('#phone1').val()
        let userPass = $('#code1').val()
        if(userName && userPass){
            let data = new URLSearchParams()
            data.append('userName', userName)
            data.append('userPass', userPass)
            ajaxPost("http://localhost:8090/user/login", data, (respData)=>{
                $('#message').text(respData.data);
                if(respData.status=="MS200"){
                    window.location.href = "second-main.html?userName=" + userName
                }
            }, (err)=>{
                console.log(err)
            })
        } else{
            $('#message').text("账号或密码不能为空");
        }
        
    }
    function register(){
        if(!(checktel() && checkpass1())){
            return
        }
        let pass = $('#pass').val()
        let repassword = $('#repassword').val()
        if(pass != repassword){
            alert("密码不一致")
            return
        }
        let isChecked = $("#myCheckbox").prop("checked");
        if (!isChecked) {
            alert("请同意《米哈游用户协议》 《米哈游隐私政策》");
            return
        }
        let phone = $('#phone1').val()
        let data = new URLSearchParams()
        data.append('userName', phone)
        data.append('userPass', pass)
        ajaxPost("http://localhost:8090/user/add", data, (respData)=>{
            alert(respData.data)
        },(err)=>{
            console.log(err)
        })
    }
    $('#input2').on("click", login)
    $('#input1').on("click", register)
})